const sid=wx.getStorageSync('id') 
let id=""
let db=wx.cloud.database()
let message={}
const zhuangtai=["关注","已关注"]
const color=["#ccc","#bb0404"]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list:{},
    text:"关注",
    state:"bb0404"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      id=options.id
      db.collection("jiangrenji").doc(id).get()
      .then(res=>{
        console.log("请求成功",res);
        this.setData({
          list:res.data
        })
      })
      .catch(err=>{
        console.log("请求失败",err);
      })

      db.collection("student").doc(sid).get().then(res=>{
        console.log(res);
        message=res.data
        if(message.guanzhuarr.includes(id)){
          this.setData({
            state:"#ccc",
            text:"已关注"
          })
        }
        else{
          this.setData({
            state:"#bb0404",
            text:"关注"
          })
        }
        console.log(message);
      })
  },
  click(){
    if(this.data.state=="#ccc"){
      this.setData({
        state:"bb0404",
        text:"关注"
      })
      message.guanzhunum=message.guanzhunum-1;
      message.guanzhuarr.push(id)
      let myArray = ["apple", "banana", "orange", "grape"];
      message.guanzhuarr = message.guanzhuarr.filter(function(item) {
  return item !== id
});
      db.collection("student").doc(sid).update({
        data:{
          guanzhunum:message.guanzhunum,
          guanzhuarr:message.guanzhuarr
        }
      }).then(res=>{
        console.log(res);
      })
    }
    else{
      this.setData({
        state:"#ccc",
        text:"已关注"
      })
      wx.showToast({
        title: '关注成功',
      })
      if(!message.guanzhuarr.includes(id)){
      message.guanzhunum=message.guanzhunum+1;
      message.guanzhuarr.push(id)
      db.collection("student").doc(sid).update({
        data:{
          guanzhunum:message.guanzhunum,
          guanzhuarr:message.guanzhuarr
        }
      }).then(res=>{
        console.log(res);
      })
    }
    }
  },
  onChange(event) {
    console.log(event)
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.title}`,
    //   icon: 'none',
    // });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})